#!/usr/bin/env bash

rsdk-help-rsdk() {
	cat <<EOF
rsdk
====

Supported subcommands:
$("$SCRIPT_DIR/../../bin/rsdk" config completions.supported_subcommands)
EOF
}

rsdk-help-build() {
	cat <<EOF
rsdk-${FUNCNAME[0]##rsdk-help-}
====

Build RadxaOS system image.

## Command usage

    $ rsdk build [options] <product> [suite] [edition]

## Options

    --no-cache
        Do not use cached build artifacts. This will result in rootfs rebuild.
    --no-efi
        Do not create EFI partition in the generated image.
    -d | --debug
        Enable debug output. This will also build rootfs into a folder for easier
        examination.
    -T | --test-repo
        Build using Radxa APT test archives.
    -m | --mirror <mirror_url>
        Specify custom Debian / Ubuntu archive mirror URL.
    -M <mirror_url>
        Specify custom non-Debian non-Ubuntu archive mirror URL. This mirror should
        serve both Radxa archives as well as any other 3rd party archives.

        The following archives will use this mirror:
$(find "$SCRIPT_DIR/../../../externals/keyrings/" -maxdepth 1 -mindepth 1 -type d ! -name debian -a ! -name ubuntu -printf "            %f\n")
    -i | --image-name <image_name>
        Set the system image name. (Default: $("$SCRIPT_DIR/../../bin/rsdk" config build.default_image_name))
    -h | --help
        Show this message.
    -k | --override-kernel
        Override the default kernel profile. Ex: latest, rockchip, rk2312, etc.
    -f | --override-firmware
        Override the default firmware profile. Ex: latest, rknext, etc.
    -p | --override-product
        Override the default product profile. Ex: rock-4se, radxa-zero, etc
    --no-vendor-packages
        Do not install the vendor package specified in product profile.
    --debs <debs_dir>
        Include <debs_dir> as the local package archive.
        The packages in this archive will be preferred over all other archives.
        Packages will be kept even after the build is completed, effectively
        disabling any future upgrades on the included packages.
        WARNING: the entire content of <debs_dir> will be included! 
EOF
}

rsdk-help-chroot() {
	echo "rsdk-${FUNCNAME[0]##rsdk-help-}"
	cat <<'EOF'
====

Enter chroot environment for a given disk or disk image.

Requires `systemd-nspawn` (provided by `systemd-container` package in Debian).

## Command usage

    $ rsdk chroot <target path>

## Supported target type

* Raw block devices (i.e. ordinary disks)
* System image file
  * Requires `kpartx` (provided by `multipath-tools` package in Debian)

Following distributions may be used in addition to `rsdk` image:
* `debox-radxa` image
* `rbuild` image
* `rsdk` image
* `Armbian` image
* Orange Pi image
* Rockchip SDK image
EOF
}

rsdk-help-install() {
	echo "rsdk-${FUNCNAME[0]##rsdk-help-}"
	cat <<'EOF'
====

Install a build artifacts to a given disk or disk image.

Requires `systemd-nspawn` (provided by `systemd-container` package in Debian).

## Command usage

    $ rsdk install <target path> <artifact path>

## Supported target type

* Raw block devices (i.e. ordinary disks)
* System image file
  * Requires `kpartx` (provided by `multipath-tools` package in Debian)

## Supported artifact type

* `*.deb`: Debian package
* `*.dtb`: Device tree file
* `*.dtbo`: Device tree overlay file

Only `rsdk` image supports all artifact types.
Other distributions may have issues on some artifact types. 
EOF
}

main() {
	local SCRIPT_DIR
	SCRIPT_DIR="$(dirname "$(realpath "$0")")"
	# shellcheck source=src/lib/rsdk/utils.sh
	source "$SCRIPT_DIR/../../lib/rsdk/utils.sh"

	local subcommand="${1:-rsdk}"
	shift || true
	"rsdk-help-$subcommand" "$@"
}

main "$@"
